package online.inote.common.utils;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;

import lombok.Getter;

/**
 * <p>
 * Desc: 返回值工厂类
 * </p>
 * 
 * @author 	XQF SUI
 * @Created 2016年11月18日 下午11:30:53
 * @version 1.0
 */
public class ResultFactory {

	public static final String RESULT_KEY = "result_code";
	public static final String RESULT_MES = "result_message";
	public static final String RESULT_DATA_KEY = "result_data";

	public static Map<String, Object> createResultMap() {
		return new HashMap<String, Object>();
	}
	
	public static Map<String, Object> initParams(Map<String, Object> resultMap, ResultEnum result) {
		resultMap.put(RESULT_KEY, result.key);
		resultMap.put(RESULT_MES, result.msg);
		return resultMap;
	}
	
	public static Map<String, Object> createResultMap(ResultEnum result) {
		return initParams(createResultMap(), result);
	}
	
	public static Map<String, Object> createResultMap(ResultEnum result, String customizedMessage) {
		Map<String, Object> resultMap = createResultMap(result);
		resultMap.put(RESULT_MES, customizedMessage);
		
		return resultMap;
	}
	
	@Deprecated
	public static Map<String, Object> createResultMap(ResultEnum result, Map<String, Object> resultMap) {
		resultMap.put(RESULT_KEY, result.key);
		resultMap.put(RESULT_MES, result.msg);
		
		return resultMap;
	}
	
	public static Map<String, Object> createResultMap(ResultEnum result, Object o) {
		Map<String, Object> resultMap = createResultMap(result);
		resultMap.put(RESULT_DATA_KEY, o);
		
		return resultMap;
	}
	
	public static Map<String, Object> createResultMap(ResultEnum resultEnum, String customizedMessage, Object... o) {
		return createResultMap(resultEnum, StringUtils.initMessage(customizedMessage, o));
	}
	
	public static Map<String, Object> createResultMap(String customizedCode, String customizedMessage) {
		Map<String, Object> resultMap = createResultMap();
		resultMap.put(RESULT_KEY, customizedCode);
		resultMap.put(RESULT_MES, customizedMessage);
		
		return resultMap;
	}
	
	public static Map<String, Object> createResultMap(String customizedCode, String customizedMessage, Object o) {
		Map<String, Object> resultMap = createResultMap();
		resultMap.put(RESULT_KEY, customizedCode);
		resultMap.put(RESULT_MES, customizedMessage);
		resultMap.put(RESULT_DATA_KEY, o);
		
		return resultMap;
	}
	
	public static String createResultJSON(ResultEnum result) {
		return JSON.toJSONString(createResultMap(result));
	}
	
	public static String createResultJSON(ResultEnum result, String customizedMessage) {
		return JSON.toJSONString(createResultMap(result, customizedMessage));
	}
	
	public static String createResultJSON(ResultEnum result, Object o) {
		return JSON.toJSONString(createResultMap(result, o));
	}
	
	public static String createResultJSON(ResultEnum result, String customizedMessage, Object... o) {
		return JSON.toJSONString(createResultMap(result, customizedMessage, o));
	}
	
	public static String createResultJSON(String customizedCode, String customizedMessage) {
		return JSON.toJSONString(createResultMap(customizedCode, customizedMessage));
	}
	
	public static String createResultJSON(String customizedCode, String customizedMessage, Object o) {
		return JSON.toJSONString(createResultMap(customizedCode, customizedMessage, o));
	}
	
	/**
	 * <p>
	 * Desc: 结果枚举类(可根据需求考虑单独出去)
	 * </p>
	 * 
	 * @author 	XQF SUI
	 * @Created 2016年11月19日 下午3:42:09
	 * @version 1.0
	 */
	@Getter
	public enum ResultEnum {
		
		/**
		 * 成功(A10001)
		 */
		SUCCESS("A10001", "成功"),
		
		/**
		 * 失败(A10002)
		 */
		FAIL("A10002", "失败"),
		
		/**
		 * 
		 */
		PARAM_REQUIRED("A10003", "参数必填"),
		
		/**
		 * 不存在(A10004)
		 */
		NOT_EXISTS("A10004", "该信息不存在"),
		
		/**
		 * 已存在(A10005)
		 */
		EXISTS("A10005", "该信息已存在"),
		
		/**
		 * 有权限(A10006)
		 */
		OWN_PERMISSION("A10006", "有权限"),
		
		/**
		 * 没有权限(A10007)
		 */
		NOT_PERMISSION("A10007", "没有权限");
		
		private String key;
		private String msg;
		
		ResultEnum(String key, String msg) {
			this.key = key;
			this.msg = msg;
		}
		
		public ResultEnum setMsg(String msg) {
			this.msg = msg;
			return this;
		}
	}
	
	/**
	 * <p>
	 * Desc: 是否成功
	 * </p>
	 *
	 * @author XQF SUI
	 * @created 2018年8月28日 下午5:43:11
	 * @param resultMap
	 * @return
	 */
	public static boolean isSuccess(Map<String, Object> resultMap) {
		if (StringUtils.equals(MapUtils.getString(resultMap, RESULT_KEY), ResultEnum.SUCCESS.getKey())) {
			return true;
		}
		
		return false;
	}
}